//
//  LXOrderViewController.m
//  跑跑快销
//
//  Created by linxian on 14-5-8.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "LXOrderViewController.h"

@interface LXOrderViewController ()

@end

@implementation LXOrderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"下单";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    LXOrderView *orderView = [[LXOrderView alloc] init];
    self.view = orderView;
    _orderView = orderView;
    
    self.navigationController.navigationBar.translucent = NO;
    
    [self addBarButtons];
    [self initTableView];
    [self initButtons];
    
}

#pragma mark - 初始化oTableView

- (void)initTableView
{
    _orderView.oTableView.dataSource = self;
    _orderView.oTableView.delegate = self;
    
}

- (void)initButtons
{
    [_orderView.addGood addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
    [_orderView.scanGood addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addBarButtons
{
    // 左侧"搜索"按钮
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setTitle:@" " forState:UIControlStateHighlighted];
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [searchButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [searchButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighlighted];
    searchBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [searchBtn addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    searchBtn.tag = 1;
    UIBarButtonItem * searchButton = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    
    [self.navigationItem setLeftBarButtonItem:searchButton];
    
    // “退出”按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    leftButton.backgroundColor = [UIColor blueColor];
    [leftButton setTitle:@"退出" forState:UIControlStateNormal];
    [leftButton setTitle:@" " forState:UIControlStateHighlighted];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighlighted];
    leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [leftButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    leftButton.tag = 2;
    /*
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    leftView.backgroundColor =  [UIColor redColor];
    [leftView addSubview:leftButton];
    */
    UIBarButtonItem * fliterButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    
    // “下一步”按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    rightButton.backgroundColor = [UIColor blueColor];
    [rightButton setTitle:@"继续" forState:UIControlStateNormal];
    [rightButton setTitle:@" " forState:UIControlStateHighlighted];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighligh ted];
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [rightButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    rightButton.tag = 3;
    /*
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    rightView.backgroundColor =  [UIColor redColor];
    [rightView addSubview:rightButton];
     */
    UIBarButtonItem * mapButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    NSArray *btns = [NSArray arrayWithObjects:mapButton, fliterButton, nil];
    [self.navigationItem setRightBarButtonItems:btns];
    
}

- (void)down:(UIBarButtonItem *)sender
{
    switch (sender.tag) {
        case 1:
        {
            // 搜索
            NSLog(@"搜索");
            [_orderView showSearchView];
        }
            break;
        case 2:
        {
            // 退出
            NSLog(@"退出");
            UIViewController * rootVC = [self.navigationController.viewControllers objectAtIndex:1];
            [self.navigationController popToViewController:rootVC animated:YES];
        }
            break;
        case 3:
        {
            // 继续
            NSLog(@"继续");
            LXOrderSumViewController *osVC = [[LXOrderSumViewController alloc] init];
            [self.navigationController pushViewController:osVC animated:YES];
        }
            break;
        case 4:
        {
            // 添加货品规格
            NSLog(@"添加货品规格");
        }
            break;
        case 5:
        {
            // 扫描货品
            NSLog(@"扫描货品");
        }
            break;
        default:
            break;
    }
}

#pragma mark - tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"LXFormCell";
    LXFormCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LXFormCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [_orderView showAddView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
